/*
 *Createdby JFormDesigner on Tue Jul 0202:11:35CST2024
 */

package scct.zhang.ui;

import java.awt.*;
import javax.swing.*;
import scct.zhang.dao.ContactDao;
import scct.zhang.dao.UserDao;
import scct.zhang.model.Contact;
import scct.zhang.model.User;

import java.awt.event .*;
import java.util.List;

/**
 * @author张
 */
public class DlgContactUpdate extends JDialog {
    int id;
    public DlgContactUpdate(Window owner, int id) {
        super(owner);
        this.id = id;
        initComponents();
        initView();
    }

    private void initView() {
//根据id查询用户，把用户的信息添加到
        UserDao userDao = new UserDao();
        List<User> allUser = userDao.getAllUser();
        for (User user:allUser) {
            comUser.addItem(user.getUsername());
        }
        ContactDao contactDao = new ContactDao();
        Contact contact = contactDao.getContactId(id);
        txtContact.setText(contact.getName());
        txtRelationship.setText(contact.getRelationship());
    }

    private void btnUpdate(ActionEvent e) {
//TODO add your code here
        String name = txtContact.getText();
        String username = (String) comUser.getSelectedItem();
        String relationship = txtRelationship.getText();
        if (name.isEmpty() || !name.matches("[a-zA-Z0-9_\\u4e00-\\u9fa5]{1,10}$")) {
            JOptionPane.showMessageDialog(this, "请输入合法用户名");
            return;
        }
        if (relationship.isEmpty()) {
            JOptionPane.showMessageDialog(this, "关系不能为空");
            return;
        }
        ContactDao contactDao = new ContactDao();
        if (contactDao.updateContact(id, name, username, relationship)) {
            JOptionPane.showMessageDialog(this, "修改成功");
        }
        this.dispose();
    }

    private void btnCancle(ActionEvent e) {
        // TODO add your code here
        this.dispose();
    }


    private void initComponents() {
// JFormDesigner - Component initialization - DO NOT MODIFY //GEN-BEGIN:initComponents @formatter:off
// Generated using JFormDesigner Evaluation license - zhang
label3 = new JLabel();
btnUpdate = new JButton();
btnCancle = new JButton();
txtRelationship = new JTextField();
label1 = new JLabel();
comUser = new JComboBox();
label4 = new JLabel();
txtContact = new JTextField();

//======== this ========
setTitle("\u4fee\u6539\u8054\u7cfb\u4eba");
var contentPane = getContentPane();
contentPane.setLayout(null);

//---- label3 ----
label3.setText("\u5173\u7cfb");
contentPane.add(label3);
label3.setBounds(70, 155, 35, 17);

//---- btnUpdate ----
btnUpdate.setText("\u6dfb\u52a0");
btnUpdate.addActionListener(e -> btnUpdate(e));
contentPane.add(btnUpdate);
btnUpdate.setBounds(new Rectangle(new Point(95, 275), btnUpdate.getPreferredSize()));

//---- btnCancle ----
btnCancle.setText("\u53d6\u6d88");
btnCancle.addActionListener(e -> btnCancle(e));
contentPane.add(btnCancle);
btnCancle.setBounds(new Rectangle(new Point(255, 275), btnCancle.getPreferredSize()));
contentPane.add(txtRelationship);
txtRelationship.setBounds(145, 150, 145, txtRelationship.getPreferredSize().height);

//---- label1 ----
label1.setText("\u7528\u6237\u540d");
contentPane.add(label1);
label1.setBounds(new Rectangle(new Point(70, 95), label1.getPreferredSize()));
contentPane.add(comUser);
comUser.setBounds(140, 91, 145, comUser.getPreferredSize().height);

//---- label4 ----
label4.setText("\u8054\u7cfb\u4eba");
contentPane.add(label4);
label4.setBounds(new Rectangle(new Point(70, 50), label4.getPreferredSize()));
contentPane.add(txtContact);
txtContact.setBounds(140, 40, 145, txtContact.getPreferredSize().height);

{
    // compute preferred size
    Dimension preferredSize = new Dimension();
    for(int i = 0; i < contentPane.getComponentCount(); i++) {
        Rectangle bounds = contentPane.getComponent(i).getBounds();
        preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
        preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
    }
    Insets insets = contentPane.getInsets();
    preferredSize.width += insets.right;
    preferredSize.height += insets.bottom;
    contentPane.setMinimumSize(preferredSize);
    contentPane.setPreferredSize(preferredSize);
}
setSize(400, 390);
setLocationRelativeTo(getOwner());
//JFormDesigner-Endofcomponentinitialization//GEN-END:initComponents@formatter:on
    }

//JFormDesigner-Variablesdeclaration-DONOTMODIFY//GEN-BEGIN:variables@formatter:off
// Generated using JFormDesigner Evaluation license - zhang
private JLabel label3;
private JButton btnUpdate;
private JButton btnCancle;
private JTextField txtRelationship;
private JLabel label1;
private JComboBox comUser;
private JLabel label4;
private JTextField txtContact;
//JFormDesigner-Endofvariablesdeclaration//GEN-END:variables@formatter:on
}
